package com.cheyun.netsalev3.viewmodel

import android.app.Activity
import android.content.Intent
import android.view.View
import com.cheyun.netsalev3.repository.LoginRepository
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ObservableField
import androidx.lifecycle.MutableLiveData
import com.cheyun.netsalev3.bean.ChannelApplyList
import com.cheyun.netsalev3.bean.ChannelListParam
import com.cheyun.netsalev3.bean.NameIdParam
import com.cheyun.netsalev3.bean.dms.DmsUser
import com.cheyun.netsalev3.repository.SwitchingDealersRepository
import com.cheyun.netsalev3.utils.*
import com.cheyun.netsalev3.view.LoginActivity
import com.cheyun.netsalev3.widget.PopDialog
import java.util.ArrayList


/**
 * @author Michael
 * @date  2018/12/6 - 10:48.
 */
class BindDistributorActivityViewModel : BaseViewModel(), PopDialog.PopDialogListener {

    var dlrCode: String = ""
    var customerCode:String = ""
    var customerName = ObservableField<String>("")

    var customerlist = MutableLiveData<List<NameIdParam>>()

    var channellist = MutableLiveData<List<ChannelApplyList>>()

    var single:Boolean = false

    private var dialog: PopDialog? = null

    private var loginRepository = LoginRepository()
    private var switchingDealersRepository = SwitchingDealersRepository()

    var activity: Activity? = null

    init {
        getChannelList()
    }

    fun getChannelList(){
        if(MySharedPreferences.getLoginData()!=null && MySharedPreferences.getLoginData()?.member !=null) {
            var fail: () -> Unit = {
                channellist.postValue(ArrayList<ChannelApplyList>())
            }
            switchingDealersRepository.getApply(MySharedPreferences.getLoginData()?.member?.mid!!, this::setChannelList,fail)
        }
    }
    fun onClickRegister(view: View) {
        if(CommonFunc.isBlank(dlrCode)){
            MyToast("请输入经销商代码")
            return
        }
        loginRepository.bindDistributor(dlrCode, this::setData)
    }

    private var view:View? = null
    fun onCustomerClick(view:View){
        if(CommonFunc.isBlank(dlrCode)){
            MyToast("请输入经销商代码")
            return;
        }
        this.view = view
        loginRepository.getDmsUser(dlrCode,this::setDmsUser)
    }

    fun onClickCancel(view:View,item:ChannelApplyList){
        var moth: () -> Unit = {
            MyToast("撤销申请成功")
            getChannelList()
        }
        switchingDealersRepository.cancelJoin(item.id.toString(),moth)
    }
    fun onJoin(view:View){
        var context = view.context
        if (context is AppCompatActivity) {
            if (dialog == null) {
                dialog = PopDialog()
                dialog!!.listener = this
            }
            dialog?.show(context.supportFragmentManager, "pop")
        }
    }


    /**
     * 登陆请求数据回调
     */
    fun setData(param: Any) {
        MyToast("申请成功，请等待审核")
        if(!single){
            this.activity?.finish()
        }else{
            getChannelList()
        }
    }

    fun setDmsUser(list: List<DmsUser>) {
        var temlist4 = ArrayList<NameIdParam>()
        for(user in list){
            temlist4.add(NameIdParam(user.Ename+"("+user.UserName+")", user.id.toString(),0,user.UserName))
        }
        customerlist.postValue(temlist4)
        var context = this.view?.context
        if (context is AppCompatActivity) {
            if (dialog == null) {
                dialog = PopDialog()
                dialog!!.listener = this
            }
            dialog?.show(context.supportFragmentManager, "pop")
        }
    }

    fun setChannelList(list: List<ChannelApplyList>) {
        channellist.postValue(list)
    }


    fun longOut(){
        MySharedPreferences.clearAll()
        var intent = Intent(this.activity, LoginActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        this.activity?.startActivity(intent)
        MyToast("退出成功")
    }

    override var list: MutableLiveData<List<NameIdParam>>
        get(){
            return customerlist
        }
        set(value) {list = value}
    override var dialogType: MutableLiveData<String>
        get() {return MutableLiveData("edit")}
        set(value) {}
    override var dialogTitle: MutableLiveData<String>
        get(){return MutableLiveData("请输入经销商编码")}
        set(value) {}

    override fun onClickItem(view: View, param: NameIdParam) {
        customerlist.value?.apply {
            for (item in this) {
                item.IsSelect = param.value == item.value
            }
        }
        customerCode = param.value!!
        customerName.set(param.name)

        dialog?.dismiss()
    }

    override fun onDialogSubmit(view: View, param: String) {
        dlrCode = param
        onClickRegister(view)
        dialog?.dismiss()
    }

}